part of _;

extension on ChatPage {
  void addUploadMessages(
    List<String> filePaths,
    Enum_MessageType messageType, {
    Future<MessageModel> Function(MessageModel messageModel)? onMessagePrepare,
    Future<String> Function(String content)? onSendContent,
  }) async {
    for (final filePath in filePaths) {
      var messageModel = MessageModel.fromUpload(
        userModel: globalUserModel!,
        content: filePath,
        messageType: messageType,
      );
      controller_addMessages(messages: [messageModel]);
      controller_scrollToBottom();
      final prepareMessageModel = await onMessagePrepare?.call(messageModel);
      if (prepareMessageModel != null) messageModel = prepareMessageModel;
      YOSS.upload(
        filepaths: [filePath],
        dir: messageType.ossDir,
        onProgress: (fileIndex, count, total) {
          if (count < total) {
            messages.firstWhere((element) => element.id == messageModel.id).progress_upload = count / total * 100.0;
            setState(() {});
          }
        },
        onFinish: (isSuccess, urls, error) async {
          if (!isSuccess) {
            messages.firstWhere((element) => element.id == messageModel.id)
              ..messageState = YIMEnum_MessageState.Fail
              ..progress_upload = -1;
            setState(() {});
            return;
          }
          controller_sendMessage(
            messageType,
            onSendContent == null ? urls!.first : await onSendContent.call(urls!.first),
            onSendStart: (yimMessage) {
              messages[messages.indexWhere((element) => element.id == messageModel.id)] = MessageModel.fromYIMMessage(yimMessage, true)..progress_upload = 100;
              setState(() {});
              controller_scrollToBottom();
            },
          );
        },
      );
    }
    setState(() {});
    controller_scrollToBottom();
  }
}
